<template>
  <div>
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" title="异步任务队列" @del="del" @newCreate="newCreate"></v-tableTitle>
    <GTable ref="table" :hasCheck="true" :hasEdit="true" :api="apiMap.getListApi" :columnSource="baseColumns" :cbFun="cbFun" @selection-change="data => tableSelectData = data"
      @setdialog="setdialog" />

    <el-dialog :title="dialogModule.title" :visible.sync="dialogModule.flag" center v-dialogDrag width="720px" @closed="closed">
      <div v-tit="'异步任务队列'">
        <GForm ref="detail" :config="formConfig" :rules="formRules" labelWidth="120px" v-model="form"></GForm>
      </div>
      <span slot="footer">
        <el-button type="primary" @click="handleConfirm">确定</el-button>
        <el-button @click="dialogModule.flag = false">取消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import tableTitle from 'commons/TableTitle'

export default {
  name: 'execute',
  data() {
    return {
      tableSelectData: [],
      dialogModule: {
        loading: false,
        flag: false,
        title: '',
        data: null,
        type: ''
      },
      form: {
        taskNo:"",
        apiMappingId: null,
        executeMsg: '',
        executeCount: '',
        remark: ''
      },
      options: {
        apiMapping: [],
        status: []
      }
    }
  },
  computed: {
    btnConfig() {
      return [
        // {
        //   label: this.$t('common.create'),
        //   invalid: 'newCreate'
        // },
        // {
        //   label: this.$t('common.delete'),
        //   disabled: true,
        //   invalid: 'del'
        // }
      ]
    },
    baseColumns() {
      return [
        {
          label: '任务编号',
          prop: 'taskNo',
          input: 'input'
        },
        {
          label: '对接场景',
          prop: 'apiMappingId',
          input: 'select',
          options: this.options.apiMapping
        },
        {
          label: '任务执行状态',
          prop: 'executeStatus',
          input: 'select',
          options: this.options.status
        },
        {
          label: '任务执行消息',
          prop: 'executeMsg',
          input: 'input'
        },
        {
          label: '执行次数',
          prop: 'executeCount',
          input: 'input'
        },
        {
          label: '备注',
          prop: 'remark'
        },
        {
          label: this.$t('app.addName'),
          prop: 'insUser'
        },
        {
          label: this.$t('app.addDate'),
          prop: 'insTime'
        },
        {
          label: this.$t('app.updateName'),
          prop: 'updUser'
        },
        {
          label: this.$t('app.updateDate'),
          prop: 'updTime'
        }
      ]
    },
    formConfig() {
      return [
        {
          label: '任务编号',
          prop: 'taskNo',
          type: 'input'
        },
        {
          prop: 'apiMappingId',
          label: '对应场景',
          type: 'select',
          options: this.options.apiMapping
        },
        {
          prop: 'executeMsg',
          type: 'input',
          label: '任务执行消息'
        },
        {
          prop: 'executeCount',
          type: 'number',
          label: '执行次数'
        },
        {
          prop: 'remark',
          type: 'textarea',
          label: '备注'
        }
      ]
    },
    formRules() {
      return {
        taskNo:[
          {
            required: true,
            message: '请输入任务编号',
            trigger: 'blur'
          }
        ],
        apiMappingId: [
          {
            required: true,
            message: '请选择对应场景',
            trigger: 'change'
          }
        ],
        executeMsg: [
          {
            required: true,
            message: '请输入任务执行消息',
            trigger: 'blur'
          }
        ],
        executeCount: [
          {
            required: true,
            message: '请输入执行次数',
            trigger: 'blur'
          }
        ]
      }
    },
    apiMap() {
      return {
        getListApi: '/v1/edi/asyntask/selectPlTaskQueueList',
        getInfoApi: '/v1/edi/asyntask/selectPlTaskQueueById',
        deleteApi: '/v1/edi/asyntask/deletePlTaskQueueByIds',
        editApi: '/v1/edi/asyntask/editPlTaskQueueById',
        addApi: '/v1/edi/asyntask/addPlTaskQueue'
      }
    }
  },
  methods: {
    handleConfirm() {
      this.$refs.detail.validate().then(valid => {
        const params = this._.cloneDeep(this.form)
        // const appObj = this.options.app.find(
        //   opt => opt.id == params.applicationId
        // )
        // params.applicationNo = appObj.applicationNo
        // params.applicationName = appObj.applicationName
        let promise
        if (this.dialogModule.type == 'edit') {
          promise = this.$post(this.apiMap.editApi, params)
        } else {
          promise = this.$post(this.apiMap.addApi, params)
        }
        promise.then(res => {
          this.$message.success(res.returnMgr)
          this.getTableData()
          this.$refs.detail.resetFields()
          this.dialogModule.flag = false
        })
      })
    },
    getInfoById(id) {
      this.$post(this.apiMap.getInfoApi, {
        id
      }).then(res => {
        this.form = res.data.item
        this.dialogModule.flag = true
      })
    },
    setdialog(data) {
      this.dialogModule.title = `编辑[${data.applicationName}]异步任务`
      this.dialogModule.type = 'edit'
      this.dialogModule.data = data
      this.getInfoById(data.id)
    },
    newCreate() {
      this.dialogModule.title = '新增异步任务'
      this.dialogModule.type = 'add'
      this.dialogModule.data = null
      this.dialogModule.flag = true
    },
    //删除
    async del(data) {
      this.$post(this.apiMap.deleteApi, data).then(res => {
        this.$message.success(res.returnMgr)
        this.getTableData()
      })
    },
    getOptions() {
      this.$post('/v1/edi/PlApiMapping/selectPlApiMappingList', {
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        const data = res.data.page.records
        this.options.apiMapping = data.map(i => {
          i.label = i.apiMappingName
          i.value = i.id
          return i
        })
      })
    },
    closed() {
      this.$refs.detail?.resetFields()
    },
    cbFun(res) {
      return { list: res.data.page.records, total: res.data.page.total }
    },
    async getTableData() {
      this.$refs.table.getTableData()
    }
  },
  mounted() {
    this.getOptions()
  },
  components: {
    'v-tableTitle': tableTitle
  }
}
</script>

<style scoped>
</style>
